package participatr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.data.Reference;

public class Group {

	private User owner;
	private String description;
	private List<User> users;
	private String name;
	private boolean restrict;
	private Reference uri;

	public Group() {
	}

	public Group(String name, User owner) {
		this.owner = owner;
		this.name = name;
		this.restrict = true;
		this.getUserList().add(owner);// adds this user to the group
		owner.addGroup(this);// adds this group to the user
	}

	public List<User> getUserList() {
		if (this.users == null)
			this.users = new ArrayList<User>();
		return this.users;
	}

	public int getMemberCount() {
		int count = 0;
		for (User user : getUserList()) {
			count++;
		}
		return count;
	}

	public void addUser(User user) {
		this.getUserList().add(user);
		// user.addGroup(this);
	}

	public User getGroupUser(String email) {
		for (User user : getUserList()) {
			if (user.getEmail().equals(email))
				return user;
		}
		return null;

	}

	public User getGroupUser(User user) {
		for (User u : getUserList()) {
			if (u.equals(user))
				return user;
		}
		return null;
	}

	public void setOwner(User owner) {
		this.owner = owner;
	}

	public User getOwner() {
		return this.owner;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return description;
	}

	public void setRestrict(boolean restrict) {
		this.restrict = restrict;
	}

	public boolean isRestrict() {
		return restrict;
	}

	@Override
	public boolean equals(Object that) {
		if (this == that) {
			return true;
		}
		if (that == null) {
			return false;
		}
		if (this.getClass() != that.getClass()) {
			return false;
		}

		Group group = (Group) that;
		return this.getName() == group.getName();
	}

	public Reference getURI() {
		return this.uri;
	}

	public void setURI(Reference uri) {
		this.uri = uri;
	}

	public JSONObject toJson() {
		try {
			JSONObject jsonobj = new JSONObject();
			jsonobj.put("groupURI", this.getURI());
			jsonobj.put("name", this.getName());
			jsonobj.put("description", this.getDescription());
			jsonobj.put("Owner", this.getOwner().getEmail());
			jsonobj.put("Number of Members", this.getMemberCount());
			return jsonobj;

		} catch (JSONException e) {
			return null;
		}
	}

	/* returns the Json representation of the all the users part of this group */
	public JSONArray pointToJson() {

		final JSONArray json = new JSONArray();
		for (User u : getUserList()) {
			System.out.println(u.getEmail());
			json.put(u.getEmail());
			json.put(toJson());
		}
		return json;

	}

	public StringBuilder groupSb() {
		StringBuilder sb = new StringBuilder();
		final String NL = System.getProperty("line.separator");
		sb.append("groupURI: ").append(getURI());
		sb.append(NL);
		sb.append("Name: ").append(getName());
		sb.append(NL);
		sb.append("groupDescription: ").append(getDescription());
		sb.append(NL);
		sb.append("groupOwner: ").append(getOwner().getEmail());
		sb.append(NL);
		sb.append("Members: ").append(getMemberCount());
		sb.append(NL);
		return sb;
	}

	public StringBuilder fullGroupSb() {
		StringBuilder sb = new StringBuilder();
		final String NL = System.getProperty("line.separator");
		sb.append("groupURI: ").append(getURI());
		sb.append(NL);
		sb.append("Name: ").append(getName());
		sb.append(NL);
		sb.append("groupDescription: ").append(getDescription());
		sb.append(NL);
		sb.append("groupOwner: ").append(getOwner().getEmail());
		sb.append(NL);
		sb.append("Members: ").append(getMemberCount());
		sb.append(NL);
		sb.append("Users");
		sb.append(NL);
		for (User u : getUserList()) {
			sb.append("Email:  ").append(u.getEmail());
			sb.append(NL);
		}
		return sb;

	}

	public Map<String, Object> groupDataModel() {
		Map<String, Object> gmap = new HashMap<String, Object>();
		Map<String, Object> usermap = new HashMap<String, Object>();
		gmap.put("GroupName", getName());
		gmap.put("URI", getURI());
		gmap.put("Description", getDescription());
		gmap.put("GroupOwner", getOwner().getEmail());
		gmap.put("MemberCount", getMemberCount());
		gmap.put("Members", usermap);
		for (User u : getUserList()) {
			usermap.put("Email", u.getEmail());
		}
		return gmap;
	}

	// public StringBuilder userSb() {
	// StringBuilder sb = new StringBuilder();
	// final String NL = System.getProperty("line.separator");
	// for (User u : getUserList()) {
	// sb.append("User  ").append(u.getEmail());
	// sb.append(NL);
	// }
	// return sb;
	//
	// }
}
